package com.shujia

import org.junit.{After, Before, Test}

import scala.io.Source

class Code45NewJunitCompute {
  //  def main(args: Array[String]): Unit = {
  //
  //
  //
  //  }

  var scoreList: List[(String, String, String)] = _

  var stuList: List[(String, String, Int, String, String)] = _

  @Before
  def init(): Unit = {
    scoreList = Source
      .fromFile("data/score.txt")
      .getLines()
      // 将读取到的每一行数据转换成List中的元素
      .toList
      .map(
        line => {
          val column: Array[String] = line.split(",")
          (column(0), column(1), column(2))
        }
      )

    stuList = Source
      .fromFile("data/students.txt")
      .getLines()
      // 将读取到的每一行数据转换成List中的元素
      .toList
      // 将每一行的数据进行切分，并包装
      .map(
        (line: String) => {
          val colum: Array[String] = line.split(",")
          // (id,name,age,gender,clazz)
          //  Tuple相比较Array 其元素的数据类型可以多样
          (colum(0), colum(1), colum(2).toInt, colum(3), colum(4))
        }
      )

  }


  @Test
  def computeTotalScore(): Unit = {
    scoreList.map {
      case (id, course_id, score) => {
        (id, score)
      }
    }
      .groupBy {
        case (id, score) => id
      }
      .map {
        case (id, allIDList) => {
          val totalScore: Int = allIDList.map {
            case (id, score) => score.toInt
          }.sum
          (id, (1, totalScore))
        }
      }
      .map {
        // 对于case语句可以取出嵌套的数据
        case (id, (flag, totalScore)) => {
          (id, totalScore)
        }
      }.foreach(println)

  }


  @After
  def close(): Unit = {
    println("程序执行结束...")
  }

  // 作业：求出学生偏科比较严重的学生前10名学生
  //  提示：使用方差进行计算

}
